<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;

class SessionsController extends Controller
{
    public function __construct()
    {
        //只让未登录用户访问登录页面
        $this->middleware('guest', [
           'only' => ['create']
        ]);
    }

    //用户注册页面
    public function create()
    {
        return view('sessions.create');
    }

    //用户注册操作
    public function store(Request $request)
    {
       $credentials = $this->validate($request, [
          'email' => 'required|email|max:255',
          'password' => 'required'
       ]);

       if(Auth::attempt($credentials, $request->has('remember'))){
           if(Auth::user()->activated){//检查用户帐号是否激活
               //登录成功后的相关操作
               session()->flash('success', '欢迎回来！');
               return redirect()->intended(route('users.show',[Auth::user()]));
           }else{//未激活
                Auth::logout();
                session()->flash('warning', '您的帐号未激活，请检查邮箱中的注册邮件进行激活。');
                return redirect('/');
           }
       }else{
            //登录失败后的相关操作
           session()->flash('danger', '抱歉，您的邮箱和密码不匹配！');
           return redirect()->back();

       }

        return;
    }

    public function destroy()
    {
        Auth::logout();
        session()->flash('success', '您已成功退出！');
        return redirect('login');
    }
}
